home *** CD-ROM | disk | FTP | other *** search
/ Clickx 23 / Clickx 23.iso / DATA / wordpress / wp-admin / users.php < prev    next >
Encoding:
PHP Script  |  2005-08-14  |  11.1 KB  |  333 lines

  1. <?php
  2. require_once('admin.php');
  3.  
  4. $title = __('Users');
  5. $parent_file = 'profile.php';
  6.     
  7. $wpvarstoreset = array('action');
  8. for ($i=0; $i<count($wpvarstoreset); $i += 1) {
  9.     $wpvar = $wpvarstoreset[$i];
  10.     if (!isset($$wpvar)) {
  11.         if (empty($_POST["$wpvar"])) {
  12.             if (empty($_GET["$wpvar"])) {
  13.                 $$wpvar = '';
  14.             } else {
  15.                 $$wpvar = $_GET["$wpvar"];
  16.             }
  17.         } else {
  18.             $$wpvar = $_POST["$wpvar"];
  19.         }
  20.     }
  21. }
  22.  
  23. switch ($action) {
  24. case 'adduser':
  25.     check_admin_referer();
  26.  
  27.     $user_login     = wp_specialchars(trim($_POST['user_login']));
  28.     $pass1          = $_POST['pass1'];
  29.     $pass2          = $_POST['pass2'];
  30.     $user_email     = wp_specialchars(trim($_POST['email']));
  31.     $user_firstname = wp_specialchars(trim($_POST['firstname']));
  32.     $user_lastname  = wp_specialchars(trim($_POST['lastname']));
  33.     $user_uri       = wp_specialchars(trim($_POST['uri']));
  34.         
  35.     /* checking that username has been typed */
  36.     if ($user_login == '')
  37.         die (__('<strong>ERROR</strong>: Please enter a username.'));
  38.  
  39.     /* checking the password has been typed twice */
  40.     do_action('check_passwords', array($user_login, &$pass1, &$pass2));
  41.     if ($pass1 == '' || $pass2 == '')
  42.         die (__('<strong>ERROR</strong>: Please enter your password twice.'));
  43.  
  44.     /* checking the password has been typed twice the same */
  45.     if ($pass1 != $pass2)
  46.         die (__('<strong>ERROR</strong>: Please type the same password in the two password fields.'));
  47.  
  48.     $user_nickname = $user_login;
  49.  
  50.     /* checking that the username isn't already used by another user */
  51.     $loginthere = $wpdb->get_var("SELECT user_login FROM $wpdb->users WHERE user_login = '$user_login'");
  52.     if ($loginthere)
  53.         die (__('<strong>ERROR</strong>: This username is already registered, please choose another one.'));
  54.  
  55.     /* checking e-mail address */
  56.     if (empty($_POST["email"])) {
  57.         die (__("<strong>ERROR</strong>: please type an e-mail address"));
  58.         return false;
  59.     } else if (!is_email($_POST["email"])) {
  60.         die (__("<strong>ERROR</strong>: the email address isn't correct"));
  61.         return false;
  62.     }
  63.  
  64.     $user_ID = $wpdb->get_var("SELECT ID FROM $wpdb->users ORDER BY ID DESC LIMIT 1") + 1;
  65.  
  66.     $user_nicename = sanitize_title($user_nickname, $user_ID);
  67.     $user_uri = preg_match('/^(https?|ftps?|mailto|news|gopher):/is', $user_uri) ? $user_uri : 'http://' . $user_uri;
  68.     $now = gmdate('Y-m-d H:i:s');
  69.     $new_users_can_blog = get_settings('new_users_can_blog');
  70.  
  71.     $result = $wpdb->query("INSERT INTO $wpdb->users 
  72.         (user_login, user_pass, user_nickname, user_email, user_ip, user_domain, user_browser, user_registered, user_level, user_idmode, user_firstname, user_lastname, user_nicename, user_url)
  73.     VALUES 
  74.         ('$user_login', MD5('$pass1'), '$user_nickname', '$user_email', '$user_ip', '$user_domain', '$user_browser', '$now', '$new_users_can_blog', 'nickname', '$user_firstname', '$user_lastname', '$user_nicename', '$user_uri')");
  75.  
  76.     do_action('user_register', $wpdb->insert_id);
  77.  
  78.     if ($result == false)
  79.         die (__('<strong>ERROR</strong>: Couldn’t register you!'));
  80.  
  81.     $stars = '';
  82.     for ($i = 0; $i < strlen($pass1); $i = $i + 1)
  83.         $stars .= '*';
  84.  
  85.     $user_login = stripslashes($user_login);
  86.     $message  = sprintf(__('New user registration on your blog %s:'), get_settings('blogname')) . "\r\n\r\n";
  87.     $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
  88.     $message .= sprintf(__('E-mail: %s'), $user_email) . "\r\n";
  89.  
  90.     @wp_mail(get_settings('admin_email'), sprintf(__('[%s] New User Registration'), get_settings('blogname')), $message);
  91.     header('Location: users.php');
  92. break;
  93.  
  94. case 'promote':
  95.     check_admin_referer();
  96.  
  97.     if (empty($_GET['prom'])) {
  98.         header('Location: users.php');
  99.     }
  100.  
  101.     $id = (int) $_GET['id'];
  102.     $prom = $_GET['prom'];
  103.  
  104.     $user_data = get_userdata($id);
  105.     $usertopromote_level = $user_data->user_level;
  106.  
  107.     if ($user_level <= $usertopromote_level) {
  108.         die(__('Can’t change the level of a user whose level is higher than yours.'));
  109.     }
  110.  
  111.     if ('up' == $prom) {
  112.         $new_level = $usertopromote_level + 1;
  113.         $wpdb->query("UPDATE $wpdb->users SET user_level=$new_level WHERE ID = $id AND $new_level < $user_level");
  114.     } elseif ('down' == $prom) {
  115.         $new_level = $usertopromote_level - 1;
  116.         $wpdb->query("UPDATE $wpdb->users SET user_level=$new_level WHERE ID = $id AND $new_level < $user_level");
  117.     }
  118.  
  119.     header('Location: users.php');
  120.  
  121. break;
  122.  
  123. case 'delete':
  124.  
  125.     check_admin_referer();
  126.  
  127.     $id = (int) $_GET['id'];
  128.  
  129.     if (!$id) {
  130.         header('Location: users.php');
  131.     }
  132.  
  133.     $user_data = get_userdata($id);
  134.     $usertodelete_level = $user_data->user_level;
  135.  
  136.     if ($user_level <= $usertodelete_level)
  137.         die(__('Can’t delete a user whose level is higher than yours.'));
  138.  
  139.     $post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_author = $id");
  140.     if ($post_ids) {
  141.         $post_ids = implode(',', $post_ids);
  142.         
  143.         // Delete comments, *backs
  144.         $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_post_ID IN ($post_ids)");
  145.         // Clean cats
  146.         $wpdb->query("DELETE FROM $wpdb->post2cat WHERE post_id IN ($post_ids)");
  147.         // Clean post_meta
  148.         $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id IN ($post_ids)");
  149.         // Clean links
  150.         $wpdb->query("DELETE FROM $wpdb->links WHERE link_owner = $id");
  151.         // Delete posts
  152.         $wpdb->query("DELETE FROM $wpdb->posts WHERE post_author = $id");
  153.     }
  154.  
  155.     // FINALLY, delete user
  156.     $wpdb->query("DELETE FROM $wpdb->users WHERE ID = $id");
  157.     header('Location: users.php?deleted=true');
  158.  
  159. break;
  160.  
  161. default:
  162.     
  163.     include ('admin-header.php');
  164.     ?>
  165.  
  166. <?php if (isset($_GET['deleted'])) : ?>
  167. <div class="updated"><p><?php _e('User deleted.') ?></p></div>
  168. <?php endif; ?>
  169. <div class="wrap">
  170.   <h2><?php _e('Authors') ?></h2>
  171.   <table cellpadding="3" cellspacing="3" width="100%">
  172.     <tr>
  173.     <th><?php _e('ID') ?></th>
  174.     <th><?php _e('Nickname') ?></th>
  175.     <th><?php _e('Name') ?></th>
  176.     <th><?php _e('E-mail') ?></th>
  177.     <th><?php _e('Website') ?></th>
  178.     <th><?php _e('Level') ?></th>
  179.     <th><?php _e('Posts') ?></th>
  180.     <th> </th>
  181.     </tr>
  182.     <?php
  183.     $users = $wpdb->get_results("SELECT ID FROM $wpdb->users WHERE user_level > 0 ORDER BY ID");
  184.     $style = '';
  185.     foreach ($users as $user) {
  186.         $user_data = get_userdata($user->ID);
  187.         $email = $user_data->user_email;
  188.         $url = $user_data->user_url;
  189.         $short_url = str_replace('http://', '', $url);
  190.         $short_url = str_replace('www.', '', $short_url);
  191.         if ('/' == substr($short_url, -1))
  192.             $short_url = substr($short_url, 0, -1);
  193.         if (strlen($short_url) > 35)
  194.         $short_url =  substr($short_url, 0, 32).'...';
  195.         $style = ('class="alternate"' == $style) ? '' : 'class="alternate"';
  196.         $numposts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = $user->ID and post_status = 'publish'");
  197.         if (0 < $numposts) $numposts = "<a href='edit.php?author=$user_data->ID' title='" . __('View posts') . "'>$numposts</a>";
  198.         echo "
  199. <tr $style>
  200.     <td align='center'>$user_data->ID</td>
  201.     <td><strong>$user_data->user_nickname</strong></td>
  202.     <td>$user_data->user_firstname $user_data->user_lastname</td>
  203.     <td><a href='mailto:$email' title='" . sprintf(__('e-mail: %s'), $email) . "'>$email</a></td>
  204.     <td><a href='$url' title='website: $url'>$short_url</a></td>
  205.     <td align='center'>";
  206.     if (($user_level >= 2) and ($user_level > $user_data->user_level) and ($user_data->user_level > 0))
  207.         echo " <a href=\"users.php?action=promote&id=".$user_data->ID."&prom=down\">-</a> ";
  208.     echo $user_data->user_level;
  209.     if (($user_level >= 2) and ($user_level > ($user_data->user_level + 1)))
  210.         echo " <a href=\"users.php?action=promote&id=".$user_data->ID."&prom=up\">+</a> ";
  211.     echo "</td><td align='right'>$numposts</td>";
  212.     echo '<td>';
  213.     if (($user_level >= 2) and ($user_level > $user_data->user_level))
  214.         echo "<a href='user-edit.php?user_id=$user_data->ID' class='edit'>".__('Edit')."</a>";
  215.     echo '</td>';
  216.     echo '</tr>';
  217.     }
  218.     
  219.     ?>
  220.     
  221.   </table>
  222. </div>
  223.  
  224. <?php
  225. $users = $wpdb->get_results("SELECT * FROM $wpdb->users WHERE user_level = 0 ORDER BY ID");
  226. if ($users) {
  227. ?>
  228. <div class="wrap">
  229.     <h2><?php _e('Registered Users') ?></h2>
  230.     <table cellpadding="3" cellspacing="3" width="100%">
  231.     <tr>
  232.         <th><?php _e('ID') ?></th>
  233.         <th><?php _e('Nickname') ?></th>
  234.         <th><?php _e('Name') ?></th>
  235.         <th><?php _e('E-mail') ?></th>
  236.         <th><?php _e('Website') ?></th>
  237.         <th></th>
  238.         <th></th>
  239.         <th></th>
  240.     </tr>
  241. <?php
  242. $style = '';
  243. foreach ($users as $user) {
  244.     $user_data = get_userdata($user->ID);
  245.     $email = $user_data->user_email;
  246.     $url = $user_data->user_url;
  247.     $short_url = str_replace('http://', '', $url);
  248.     $short_url = str_replace('www.', '', $short_url);
  249.     if ('/' == substr($short_url, -1))
  250.         $short_url = substr($short_url, 0, -1);
  251.     if (strlen($short_url) > 35)
  252.     $short_url =  substr($short_url, 0, 32).'...';
  253.     $style = ('class="alternate"' == $style) ? '' : 'class="alternate"';
  254. echo "\n<tr $style>
  255. <td align='center'>$user_data->ID</td>
  256. <td><strong>$user_data->user_nickname</strong></td>
  257. <td>$user_data->user_firstname $user_data->user_lastname</td>
  258. <td><a href='mailto:$email' title='" . sprintf(__('e-mail: %s'), $email) . "'>$email</a></td>
  259. <td><a href='$url' title='website: $url'>$short_url</a></td>
  260. <td align='center'>";
  261.  
  262.     if ($user_level >= 6)
  263.         echo "<a href='users.php?action=promote&id=$user_data->ID&prom=up' class='edit'>". __('Promote') . '</a>';    
  264.     echo "</td>\n";
  265.     echo '<td>';
  266.     if (($user_level >= 6) and ($user_level > $user_data->user_level))
  267.         echo "<a href='user-edit.php?user_id=$user_data->ID' class='edit'>".__('Edit')."</a>";
  268.     echo '</td><td>';
  269.     if ($user_level >= 6)
  270.         echo "<a href='users.php?action=delete&id=$user_data->ID' class='delete' onclick='return confirm(\"" . __('You are about to delete this user \n  OK to delete, Cancel to stop.') . "\")'>" . __('Delete'). '</a>';
  271.     echo '</td></tr>';
  272.  
  273. }
  274.  
  275. ?>
  276.     
  277.     </table>
  278.       <p><?php _e('Deleting a user also deletes all posts made by that user.') ?></p>
  279. </div>
  280.  
  281.     <?php 
  282.     } ?>
  283. <div class="wrap">
  284. <h2><?php _e('Add New User') ?></h2>
  285. <?php printf(__('<p>Users can <a href="%s/wp-register.php">register themselves</a> or you can manually create users here.</p>'), get_settings('siteurl')); ?>
  286. <form action="" method="post" name="adduser" id="adduser">
  287.   <table class="editform" width="100%" cellspacing="2" cellpadding="5">
  288.     <tr>
  289.       <th scope="row" width="33%"><?php _e('Nickname') ?>
  290.       <input name="action" type="hidden" id="action" value="adduser" /></th>
  291.       <td width="66%"><input name="user_login" type="text" id="user_login" /></td>
  292.     </tr>
  293.     <tr>
  294.       <th scope="row"><?php _e('First Name') ?> </th>
  295.       <td><input name="firstname" type="text" id="firstname" /></td>
  296.     </tr>
  297.     <tr>
  298.       <th scope="row"><?php _e('Last Name') ?> </th>
  299.       <td><input name="lastname" type="text" id="lastname" /></td>
  300.     </tr>
  301.     <tr>
  302.       <th scope="row"><?php _e('E-mail') ?></th>
  303.       <td><input name="email" type="text" id="email" /></td>
  304.     </tr>
  305.     <tr>
  306.       <th scope="row"><?php _e('Website') ?></th>
  307.       <td><input name="uri" type="text" id="uri" /></td>
  308.     </tr>
  309. <?php
  310. $show_password_fields = apply_filters('show_password_fields', true);
  311. if ( $show_password_fields ) :
  312. ?>
  313.     <tr>
  314.       <th scope="row"><?php _e('Password (twice)') ?> </th>
  315.       <td><input name="pass1" type="password" id="pass1" />
  316.       <br />
  317.       <input name="pass2" type="password" id="pass2" /></td>
  318.     </tr>
  319. <?php endif; ?>
  320.   </table>
  321.   <p class="submit">
  322.     <input name="adduser" type="submit" id="adduser" value="<?php _e('Add User') ?> »" />
  323.   </p>
  324.   </form>
  325. </div>
  326.     <?php
  327.  
  328. break;
  329. }
  330.  
  331. include('admin-footer.php');
  332. ?>
  333.